﻿Ext.define('app.prm.project.PrmApplyController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.prmApply',
    init: function () {
        var me = this;
    },
    submitApply: function () {
        var me = this;
        var model = this.getViewModel();
        if (!model.get('proExist')) {
            Ext.Msg.alert("提示", "当前申请项目不存在，无法提交申请");
            return false;
        }

        var a = model.get('EffectiveTime');
        var b = model.get('ExpirationTime');

        if (a == '' || b == '' || (a > b && Ext.Date.format(a, 'Y-m-d') != Ext.Date.format(a, 'Y-m-d'))) {
            Ext.SimpleTips.msg("提示", "生效时间和失效时间不对");
            return;
        }




        if (model.get('auditor').length == 0) {
            Ext.Msg.confirm("警告", "当前申请项目没有找到审核人员，确定还提交查看申请么？", function (btn) {
                if (btn == 'yes') {
                    me._submitApply();
                }
            })
        } else {
            me._submitApply();
        }
    },
    _submitApply: function () {
        App.AjaxPost("/api/services/app/projectAp/SubmitApply", this.getViewModel().data, function (response) { Ext.Msg.alert("系统提示", "已成功递交申请"); });
    },
    loadAudit: function () {
        var model = this.getViewModel();
        model.set('appId', this.getView().paramsId);
        App.AjaxGet('/api/services/app/ProjectAp/GetProApp?id=' + this.getView().paramsId, function (response) {
            console.log(response);
            model.set('projectNo', response.result.projectNo);
            model.set('effectiveTime', new Date(response.result.effectiveTime));
            model.set('expirationTime', new Date(response.result.expirationTime));
            model.set('createOn', new Date(response.result.createOn));
            model.set('appUser', response.result.appUser);
            model.set('note', response.result.appNote);
        });
    },
    passProApp: function () {
        var model = this.getViewModel();
        model.set('auditPass', true);
        App.AjaxPost("/api/services/app/projectAp/AuditProApp", this.getViewModel().data, function (response) { Ext.Msg.alert("系统提示", "已通过"); });
    },
    rejectProApp: function () { 
        var model = this.getViewModel();
        model.set('auditPass', true);
        App.AjaxPost("/api/services/app/projectAp/AuditProApp", this.getViewModel().data, function (response) { Ext.Msg.alert("系统提示", "已驳回"); });
    },
    setAddress: function () {
        var model = this.getViewModel();
        console.log(model);
        model.set('proExist', false);
        model.set('auditor', '');
        App.AjaxGet('/api/services/app/Project/GetProAppAuditUsers?projectno=' + model.get('projectNo'), function (response) {
            model.set('proExist', true);
            console.log(response);
            var json = response;
            if (json.result.length > 0) {
                var users = [];
                for (var i = 0; i < json.result.length; i++) {
                    users.push(json.result[i].userName);
                }
                console.log(users.join(","));
                model.set('auditor', users.join(","));
            } else {
                Ext.Msg.alert("提示", "没有找到相关审核人员");
            }
        });
    },
    onAdd: function () {
        var grid = this.getView();
        var win = Ext.create("app.prm.project.Edit", { title: '项目创建' });
        win.on("savesuccess", function (obj) {
            win.close();
            grid.store.load();
        });
        win.show();
    },
    onEditProject: function (grid, rowIndex, colIndex) {
        var record = grid.store.getAt(rowIndex);

        Ext.util.Cookies.set('projectinfo' + record.data.id, record.data.projectName + "(" + record.data.projectNo + ")");

        this.redirectTo('app.prm.detail.Index/' + record.data.id);
    },
    onInit: function (view) {
        App.AjaxPost('/api/services/app/permission/HasAddProject', {}, function (data) {
            view.getViewModel().set('hasAdd', !data.result);
        });
    },
    onStartSearch: function () {
        var grid = this.getView(),
        panel = grid.lookupReference("projectsearch");
        grid.store.filter = panel.form.getForm().getValues();
        grid.store.load();
    },
    onClearSearch: function () {
        var grid = this.getView(),
        panel = grid.lookupReference("projectsearch");
        panel.form.getForm().reset();
        this.onStartSearch();
    }
});
